using System;
using QAssetBundle;
using UnityEngine;
using QFramework;
using QFramework.Example;

namespace XCZProject
{
    public partial class Enemy : ViewController,IEnemy
    {
        // 学了
        public float HP = 3 ;
		
        // 移动速度
        public float MovementSpeed = 2.0f;
        
        // 死亡时消散的变色
        public Color DissolveColor=Color.yellow;

        public bool TreasureChestEnemy = false;
        
        void Start()
        {
            EnemyGenerator.EnemyCount.Value++;
        }

        private void FixedUpdate()
        {
            // 敌人处于未受伤状态时，才进行移动
            if (!mIgnoreHurt)
            {
                if (Player.Default)
                {	
                    // 获取玩家当前方向
                    var direction = (Player.Default.transform.position - transform.position).normalized;
			
                    // 向玩家方向移动
                    //transform.Translate(direction*Time.fixedDeltaTime*MovementSpeed);
                    // 用刚体触发伤害事件，避免用上面这个代码，容易在接触到敌人时触发多次伤害
                    SelfRigidbody2D.velocity = direction * MovementSpeed;
                }
                else
                {
                    SelfRigidbody2D.velocity = Vector2.zero;
                }
            }

        }

        private void Update()
        {
            if (HP<=0)
            {
                //UIKit.OpenPanel<UIGamePassPanel>();
                // TODO：添加经验值调用
                // print(gameObject.name);
                Global.GeneratePowerUp(gameObject,TreasureChestEnemy);
                FxController.Play(Sprite,DissolveColor);
                AudioKit.PlaySound(Sfx.ENEMY_DIE);
                this.DestroyGameObjGracefully();
                //Global.Exp.Value++;
            }
        }

        // 敌人是否处于受伤状态
        private bool mIgnoreHurt = false;
		
        /// <summary>
        /// 敌人受伤时调用
        /// </summary>
        /// <param name="value"></param>
        public void Hurt(float value,bool force=false,bool cirtical=false)
        {
            if (mIgnoreHurt&&!force)
            {
                return;
            }

            mIgnoreHurt = true;
            // 在受伤时，敌人不动
            SelfRigidbody2D.velocity = Vector2.zero;
			
            // toString后面输入“0”代表取整，类似excel中的的round()
            FloatingTextController.Play(transform.position+Vector3.up*1,value.ToString("0"),cirtical);
			
            Sprite.color=Color.red;
            AudioKit.PlaySound("hit");
            ActionKit.Delay(0.4f,() =>
            {
                // 敌人的血量每次减少的数量取决于主角当前的攻击力
                this.HP -= value;
                this.Sprite.color = Color.white;
                mIgnoreHurt = false;
            }).Start(this);
        }

        public void SetSpeedScale(float speedScale)
        {
            MovementSpeed *= speedScale;
        }

        public void SetHpScale(float hpScale)
        {
            HP *= hpScale;
        }

        private void OnDestroy()
        {

            EnemyGenerator.EnemyCount.Value--;
        }
    }
}